/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plechinger.mcremote.server;

import com.plechinger.mcremote.client.ClientSettings;
import com.plechinger.mcremote.security.SecurityUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Lukas
 */
public class Server {

    private static Server server = null;
    private Properties properties = new Properties();
    private Dispatcher dispatcher;

    private Server() {
        try {
            File propertiesfile = new File(System.getProperty("user.home")
                    + System.getProperty("file.separator") + ".mcremote"
                    + System.getProperty("file.separator")
                    + "mcremote_server.properties");
            if (!propertiesfile.exists()) {

                File folder = new File(propertiesfile.getParent());
                if (!folder.isDirectory()) {
                    folder.mkdir();
                }
                propertiesfile.createNewFile();

            }

            properties.load(new FileInputStream(propertiesfile));
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            initServer();
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Server getServer() {
        if (server == null) {
            server = new Server();
        }
        return server;
    }

    public File getScriptFolder() {
        return new File(properties.getProperty("server.scriptFolder", System.getProperty("user.dir") + File.separator + "scripts"));
    }

    public void startServer() {
        dispatcher.startServer();
    }

    public void stopServer() {
        dispatcher.startServer();
    }

    public int getServerPort() {
        return Integer.parseInt(properties.getProperty("mcremote.port", "29900"));
    }

    private void initServer() throws IOException {
        dispatcher = new Dispatcher(getServerPort());
    }

    public Dispatcher getDispatcher() {
        return dispatcher;
    }

    public Properties getProperties() {
        return properties;
    }

    public Key getEncryptionKey() {
        try {
            return SecurityUtil.getKey(properties.getProperty("network.encryptionKey"));
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(ClientSettings.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void main(String[] args) {
        Server server = Server.getServer();
        server.startServer();
    }
}
